<?php
namespace app\techn_api\controller\v1;
use think\Db;
use app\service\TechnWeixinService;
use app\service\GaodeService;
use think\Request;

class Publics extends Base
{
	public function _initialize()
	{
		parent::_initialize();
	}


    /**
     * 获取手机验证码
     */
    public function get_mobile_code()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        $params = $this->data_post;
        $validate = $this->validate(
            $params,
            [
                'mobile' => ['require', 'regex' => '^1((3|4|5|6|7|8|9){1}\d{1})\d{8}$']
            ],
            [
                'mobile.require' => '请输入手机号',
                'mobile.regex' => '手机格式有误'
            ]
        );
        if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        //验证码发送时间
        $send_time = time();
        $mobile_info = cache($params['mobile'].'_mobile_code');
        if(!empty($mobile_info))
        {
            //如果已经发送过
            $send_time = $mobile_info['send_time'];
        }
        //判断发送时间
        if(time() < $send_time)
        {
            echo json_encode(['code' => -1, 'msg' => '请'.($send_time - time()).'秒后重新发送']);
            exit();
        }
        $mobile_code = GetRandomStr(6, 'int');
//        Db::table('code')->insert(['code'=>$mobile_code]);
        //发送短信
        $res_sms = aliyun_send_sms("LTAI5tCfyuqK4o58i6MH3qH2","xXOnPRM4tMimqDVR4e1Ms5crn0lUtP",$params['mobile'],"太原合旭网络科技有限公司",'SMS_267295075',["code" => $mobile_code]);


        if($res_sms['Code'] == "OK")
        {
            cache($params['mobile'].'_mobile_code', ['mobile_code' => $mobile_code, 'send_time' => (time() + 60)], 600); //验证码有效期10分钟
            $result['code'] = 0;
            $result['msg'] = "短信发送成功";
        }else
        {
            $result['code'] = -1;
            $result['msg'] = "短信发送失败";
        }
        echo json_encode($result);
    }

    public function get_ip(Request $request){
        $param = $request->param();
        $user_info = Db::name('user_ip')->where('ip',$param['data_ip'])->find();
        $user = Db::name('user')->where('id',$user_info['user_id'])->find();
        if (!empty($user_info)){
            echo json_encode(['code'=>0,'msg'=>'登录','data'=>$user]);
        }else{
            echo json_encode(['code'=>-1,'msg'=>'无登录记录']);
        }
    }
    /**
     * 用户登录
     */
    public function app_login()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        $params = $this->data_post;
        $validate = $this->validate(
            $params,
            [
                'agreement' => 'require|eq:false',
                'mobile' => ['require','regex' => '^1((3|4|5|6|7|8|9){1}\d{1})\d{8}$'],
                'mobile_code' => 'require'
            ],
            [
                'agreement.require' => '请同意协议',
                'agreement.eq' => '请同意协议',
                'mobile.require' => '请填写手机号',
                'mobile.regex' => '手机格式有误',
                'mobile_code.require' => '请填写验证码'
            ]
        );
        if(true !== $validate){
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
//
        if ($params['mobile'] != 15035514195 && $params['mobile'] != 17661043348){
//        if  ($params['mobile'] != 18055130678){ //判断验证码是否正确

            //判断验证码是否正确
            $mobile_info = cache($params['mobile'].'_mobile_code');
            if(empty($mobile_info))
            {
                echo json_encode(['code' => -1, 'msg' => '请重新获取验证码']);
                exit();
            }
            if($mobile_info['mobile_code'] != $params['mobile_code'])
            {
                echo json_encode(['code' => -1, 'msg' => '验证码不正确']);
                exit();
            }
        }

//        cache($params['mobile'].'_mobile_code', NULL);


        $mobile_info['phoneNumber'] = $params['mobile'];
        $mobile = $mobile_info['phoneNumber'];
        $user_info = Db::name('user')->where(['mobile' => $mobile])->find();
        if (empty($user_info)){
            echo json_encode(['code' => -1, 'msg' => '暂未注册客户端']);
            exit();
        }
//        dump($user_info);
        //请求身份
        $identity_type = IdentityType();
        //当前应用平台
        $client_type = ApplicationClientType();
        //用户token
        $token = '';

        $youmeiyou = Db::name('user_weixin')->where('user_id',$user_info['id'])->where('identity_type',$identity_type)->where('client_type',$client_type)->find();
        if (empty($youmeiyou)){
            $user_weixin_id = Db::name('user_weixin')->insert([
                'user_id' => $user_info['id'],
                'identity_type' => $identity_type,
                'client_type' => $client_type,
                'openid' => '1',
                'add_time' => time(),
                'upd_time' => time()
            ]);

        }


        if(!empty($user_info))
        {
            //存在
            $user_id = $user_info['id'];
            //更新用户token
            $token = md5(md5($user_id.time()).rand(100, 1000000)).'_'.GetUserRecommendCode($user_id);

            // 启动事务
            Db::startTrans();
            //查询用户关联token是否存在
            $user_token_info = Db::name('user_token')->where(['user_id' => $user_id, 'identity_type' => $identity_type, 'client_type' => $client_type])->find();

            if(!empty($user_token_info))
            {
                //更新用户token
                if(!Db::name('user_token')->where(['id' => $user_token_info['id']])->update(['token' => $token, 'upd_time' => time()]))
                {
                    //回滚事务
                    Db::rollback();
                    echo json_encode(['code' => -1, 'msg' => '登录失败']);
                    exit();
                }
            }else
            {
                //插入用户关联token
                $user_token_id = Db::name('user_token')->insertGetId([
                    'user_id' => $user_id,
                    'identity_type' => $identity_type,
                    'client_type' => $client_type,
                    'token' => $token,
                    'add_time' => time(),
                    'upd_time' => time()
                ]);
//                dump($user_token_id);die;

                if($user_token_id <= 0)
                {
                    //回滚事务
                    Db::rollback();
                    echo json_encode(['code' => -1, 'msg' => '登录失败']);
                    exit();
                }
            }
            //查询用户关联微信openid是否存在
            $user_weixin_info = Db::name('user_weixin')->where(['user_id' => $user_id, 'identity_type' => $identity_type, 'client_type' => $client_type])->find();

            if(!empty($user_weixin_info))
            {
                //更新用户微信openid
                if(!Db::name('user_weixin')->where(['id' => $user_weixin_info['id']])->update(['openid' => $user_weixin_info['openid'], 'upd_time' => (time()+1)]))
                {
                    //回滚事务
                    Db::rollback();
                    echo json_encode(['code' => -1, 'msg' => '登录失败']);
                    exit();
                }
            }else
            {
                echo json_encode(['code' => -1, 'msg' => '用户暂未注册']);
                exit();
            }
            //提交事务
            Db::commit();
        }else
        {
            //不存在

            echo json_encode(['code' => -1, 'msg' => '用户暂未注册']);
            exit();

        }
        if(!empty($token))
        {
            $result['code'] = 0;
            $result['msg'] = "登录成功";
            $result['data'] = [
                'token' => $token
            ];
        }else
        {
            $result['code'] = -1;
            $result['msg'] = "登录失败";
        }

        echo json_encode($result);
    }
	/**
	 * 微信授权登录
	 */
	public function wx_login()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$validate = $this->validate(
			$params,
			[
				'agreement'     => 'require|eq:false',
				'code'          => 'require',
				'encryptedData' => 'require',
				'iv'            => 'require',
			],
			[
				'agreement.require'     => '请同意协议',
				'agreement.eq'          => '请同意协议',
				'code.require'          => '授权信息有误，请重新获取',
				'encryptedData.require' => '授权信息有误，请重新获取',
				'iv.require'            => '授权信息有误，请重新获取',
			]
		);
		if(true !== $validate)
		{
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		//先用code换取openid
		$openid_info = http_curl("https://api.weixin.qq.com/sns/jscode2session?appid=".TechnWeixinService::$mini_appid."&secret=".TechnWeixinService::$mini_appsecret."&js_code=".$params['code']."&grant_type=authorization_code");
		$openid_info = json_decode($openid_info,true);
		if(empty($openid_info))
		{
			echo json_encode(['code' => -11, 'msg' => '授权失败，请重试']);
			exit();
		}
		if(empty($openid_info['session_key']) || empty($openid_info['openid']))
		{
			echo json_encode(['code' => -1111, 'msg' => '授权失败，请重试']);
			exit();
		}
		$session_key = $openid_info['session_key'];
		$openid = $openid_info['openid'];
		$unionid = !empty($openid_info['unionid']) ? $openid_info['unionid'] : '';
		//解谜手机号码
		$aes_info = new \aes\WXBizDataCrypt(TechnWeixinService::$mini_appid, $session_key);
		$aes_code = $aes_info->decryptData($params['encryptedData'], $params['iv'], $mobile_info);
		$mobile_info = json_decode($mobile_info,true);
		if($aes_code != 0 || empty($mobile_info))
		{
			echo json_encode(['code' => -111, 'msg' => '授权失败，请重试']);
			exit();
		}
		$mobile = $mobile_info['phoneNumber'];
		$user_info = Db::name('user')->where(['mobile' => $mobile])->find();
		//请求身份
		$identity_type = IdentityType();
		//当前应用平台
		$client_type = ApplicationClientType();
		//用户token
		$token = '';
		if(!empty($user_info))
		{
			//存在
			$user_id = $user_info['id'];
			//更新用户token
			$token = md5(md5($user_id.time()).rand(100, 1000000)).'_'.GetUserRecommendCode($user_id);
			// 启动事务
			Db::startTrans();
			//查询用户关联token是否存在
			$user_token_info = Db::name('user_token')->where(['user_id' => $user_id, 'identity_type' => $identity_type, 'client_type' => $client_type])->find();
			if(!empty($user_token_info))
			{
				//更新用户token
				if(!Db::name('user_token')->where(['id' => $user_token_info['id']])->update(['token' => $token, 'upd_time' => time()]))
				{
					//回滚事务
					Db::rollback();
					echo json_encode(['code' => -1, 'msg' => '登录失败']);
					exit();
				}
			}else
			{
				//插入用户关联token
				$user_token_id = Db::name('user_token')->insertGetId([
					'user_id' => $user_id,
					'identity_type' => $identity_type,
					'client_type' => $client_type,
					'token' => $token,
					'add_time' => time(),
					'upd_time' => time()
				]);
				if($user_token_id <= 0)
				{
					//回滚事务
					Db::rollback();
					echo json_encode(['code' => -1, 'msg' => '登录失败']);
					exit();
				}
			}
			//查询用户关联微信openid是否存在
			$user_weixin_info = Db::name('user_weixin')->where(['user_id' => $user_id, 'identity_type' => $identity_type, 'client_type' => $client_type])->find();
			if(!empty($user_weixin_info))
			{
				//更新用户微信openid
				if(!Db::name('user_weixin')->where(['id' => $user_weixin_info['id']])->update(['openid' => $openid, 'upd_time' => time()]))
				{
					//回滚事务
					Db::rollback();
					echo json_encode(['code' => -1, 'msg' => '登录失败']);
					exit();
				}
			}else
			{
				//插入用户关联微信openid表
				$user_weixin_id = Db::name('user_weixin')->insertGetId([
					'user_id' => $user_id,
					'identity_type' => $identity_type,
					'client_type' => $client_type,
					'openid' => $openid,
					'add_time' => time(),
					'upd_time' => time()
				]);
				if($user_weixin_id <= 0)
				{
					//回滚事务
					Db::rollback();
					echo json_encode(['code' => -1, 'msg' => '登录失败']);
					exit();
				}
			}
			//提交事务
			Db::commit();
		}else
		{
			//不存在
			//启动事务
			Db::startTrans();
			//插入用户主表
			$user_id = Db::name('user')->insertGetId([
				'mobile' => $mobile,
				'add_time' => time(),
				'upd_time' => time()
			]);
			if($user_id <= 0)
			{
				//回滚事务
				Db::rollback();
				echo json_encode(['code' => -1, 'msg' => '登录失败']);
				exit();
			}
			//更新用户token
			$token = md5(md5($user_id.time()).rand(100, 1000000)).'_'.GetUserRecommendCode($user_id);
			//插入用户关联token
			$user_token_id = Db::name('user_token')->insertGetId([
				'user_id' => $user_id,
				'identity_type' => $identity_type,
				'client_type' => $client_type,
				'token' => $token,
				'add_time' => time(),
				'upd_time' => time()
			]);
			if($user_token_id <= 0)
			{
				//回滚事务
				Db::rollback();
				echo json_encode(['code' => -1, 'msg' => '登录失败']);
				exit();
			}
			//插入用户关联微信openid表
			$user_weixin_id = Db::name('user_weixin')->insertGetId([
				'user_id' => $user_id,
				'identity_type' => $identity_type,
				'client_type' => $client_type,
				'openid' => $openid,
				'add_time' => time(),
				'upd_time' => time()
			]);
			if($user_weixin_id <= 0)
			{
				//回滚事务
				Db::rollback();
				echo json_encode(['code' => -1, 'msg' => '登录失败']);
				exit();
			}
			//提交事务
			Db::commit();
		}
		if(!empty($token))
		{
			$result['code'] = 0;
		    $result['msg'] = "登录成功";
		    $result['data'] = [
		    	'token' => $token
		    ];
		}else
		{
			$result['code'] = -1;
			$result['msg'] = "登录失败";
		}
		echo json_encode($result);
	}

	/**
	 * 上传图片
	 */
	public function upload_images()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;

		$validate = $this->validate(
			$params,
			[
				'path' => 'require'
			],
			[
				'path.require' => '上传路径不能为空'
			]
		);
		if(true !== $validate)
		{
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		if($file = request()->file('file'))
		{
			$info = $file->validate(['size'=>15678000,'ext'=>'jpg,png,gif,jpeg'])->move('./uploads/images/'.$params['path'].'/');
			if(!empty($info))
			{
				$url = '/uploads/images/'.$params['path'].'/'.str_replace('\\', '/', $info->getSaveName());
                dump($url);die;
                $ImageSize=GetImageSize('.'.$url);
                $width=$ImageSize[0];
                $height=$ImageSize[1];
				
				if(!empty($params['types'])){

				    $image = \think\Image::open('.'.$url);

				    if($params['types']==1){//头像
//                        $num = strlen($url);
//                        $nurl = substr($url,0,$num-4).'1.jpg';
//                        $image->tilewater('./water.png',10)->save('.'.$nurl); //图片平铺水印
//                        $url = $nurl;
//                        // 给原图左上角添加水印并保存water_image.png

//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#22222280',array($width*0.5,$height*0.62),0,30)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#22222280',array($width*0.55,$height*0.66),0,30)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#22222280',array($width*0.5,$height*0.72),0,30)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#22222280',array($width*0.52,$height*0.78),0,30)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#22222280',array($width*0.56,$height*0.82),0,30)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#22222280',array($width*0.6,$height*0.86),0,30)->save('.'.$url);
				    }else{//证书
//                        $num = strlen($url);
//                        $nurl = substr($url,0,$num-4).'1.jpg';
//                        $image->tilewater('./water.png',20)->save('.'.$nurl); //图片平铺水印
//                        $url = $nurl;
//				            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.1,$height*0.03),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.3,$height*0.03),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.5,$height*0.03),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.7,$height*0.03),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.9,$height*0.03),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.2,$height*0.13),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.4,$height*0.13),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.6,$height*0.13),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.8,$height*0.13),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.1,$height*0.23),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.3,$height*0.23),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.5,$height*0.23),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.7,$height*0.23),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.9,$height*0.23),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.2,$height*0.33),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.4,$height*0.33),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.6,$height*0.33),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.8,$height*0.33),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.1,$height*0.43),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.3,$height*0.43),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.5,$height*0.43),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.7,$height*0.43),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.9,$height*0.43),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.2,$height*0.33),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.4,$height*0.33),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.6,$height*0.33),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.8,$height*0.33),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.1,$height*0.53),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.3,$height*0.53),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.5,$height*0.53),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.7,$height*0.53),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.9,$height*0.53),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.2,$height*0.63),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.4,$height*0.63),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.6,$height*0.63),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.8,$height*0.63),0,50)->save('.'.$url);
//
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.1,$height*0.73),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.3,$height*0.73),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.5,$height*0.73),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.7,$height*0.73),0,50)->save('.'.$url);
//                            $image->text('摩小妹','./static/fonts/msyh.ttf',25,'#f5f5f5',array($width*0.9,$height*0.73),0,50)->save('.'.$url);
				    }

				}

				$result['code'] = 0;
				$result['msg'] = '上传成功';
				$result['data'] = [
					'url' => $url
				];
			}else
			{
				$result['code'] = -1;
				$result['msg'] = $file->getError();
			}
		}else
		{
			$result['code'] = -1;
			$result['msg'] = "没有上传文件";
		}
		echo json_encode($result);
	}

	/**
	 * 上传视频
	 */
	public function upload_video()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$validate = $this->validate(
			$params,
			[
				'path' => 'require'
			],
			[
				'path.require' => '上传路径不能为空'
			]
		);
		if(true !== $validate)
		{
			echo json_encode(['code' => -1, 'msg' => $validate]);
			exit();
		}
		if($file = request()->file('file'))
		{
			$info = $file->validate(['size'=>15678000,'ext'=>'mp4'])->move('./uploads/video/'.$params['path'].'/');
			if(!empty($info))
			{
				$url = '/uploads/video/'.$params['path'].'/'.str_replace('\\', '/', $info->getSaveName());
				$result['code'] = 0;
				$result['msg'] = '上传成功';
				$result['data'] = [
					'url' => $url
				];
			}else
			{
				$result['code'] = -1;
				$result['msg'] = $file->getError();
			}
		}else
		{
			$result['code'] = -1;
			$result['msg'] = "没有上传文件";
		}
		echo json_encode($result);
	}

	/**
	 * 根据定位坐标获取当前详情地址
	 */
	public function loc_get_address()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		//dump($params);
		$validate = $this->validate(
			$params,
			[
				'lng' => 'require',
				'lat' => 'require'
			],
			[
				'lng.require' => '坐标有误',
				'lat.require' => '坐标有误'
			]
		);
		if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        //根据坐标获取当前省、市、区/县
        //获取高德解地址
        $ni_dili_data = http_curl('https://restapi.amap.com/v3/geocode/regeo?output=json&location='.$params['lng'].','.$params['lat'].'&key='.GaodeService::$web_api_key);
        $ni_dili_data = json_decode($ni_dili_data, true);
        //dump($ni_dili_data);
        if($ni_dili_data['status'] == 1)
        {
        	//根据行政区编码返查出省、市、区/县
        	$adcode = $ni_dili_data['regeocode']['addressComponent']['adcode'];
        	if(!empty($adcode))
        	{
        		//查询当前区/县
        		$county_info = Db::name('area')->where(['id' => $adcode, 'lev' => 3])->find();
        		//查询当前市
        		$city_info = Db::name('area')->where(['id' => $county_info['pid'], 'lev' => 2])->find();
        		//查询当前省
        		$province_info = Db::name('area')->where(['id' => $city_info['pid'], 'lev' => 1])->find();
        	}
        	//获取格式化地址
        	$address = $ni_dili_data['regeocode']['formatted_address'];
        }
        $result['code'] = 0;
	    $result['msg'] = "请求成功";
	    $result['data'] = [
        	'province' => isset($province_info['id']) ? $province_info['id'] : 0,
        	'city' => isset($city_info['id']) ? $city_info['id'] : 0,
        	'county' => isset($county_info['id']) ? $county_info['id'] : 0,
        	'province_name' => isset($province_info['name']) ? $province_info['name'] : '',
        	'city_name' => isset($city_info['name']) ? $city_info['name'] : '',
        	'county_name' => isset($county_info['name']) ? $county_info['name'] : '',
        	'address' => isset($address) ? $address : '',
        	'lng' => $params['lng'],
        	'lat' => $params['lat'],
	    ];

        $list = Db::name('user_biaoqian')->where('type',2)->select();
        if (!empty($list)){
            $result['pingjia'] = $list;
        }else{
            $result['pingjia'] = [];
        }
	    echo json_encode($result);
	}
    public function auto_update_address(Request $request){
        $param = $request->param();
        $techn = $this->techn;
        if(!empty($param)){
            $result = Db::name('techn_last')->where('techn_id',$techn['id'])->update($param);
            if ($result){
                echo json_encode(['code'=>0,'msg'=>'更新成功']);
            }else{
                echo json_encode(['code'=>-1,'msg'=>'暂无更新']);

            }
        }
    }

    public function openorclose_position(Request $request){
        $param = $request->param();
        $techn = $this->techn;
        if(!empty($param)){

           if ($param['status'] == 0){

               $result = Db::name('techn_last')->where('techn_id',$techn['id'])->update(['is_auto'=>1]);
           }else{
               $result = Db::name('techn_last')->where('techn_id',$techn['id'])->update(['is_auto'=>0]);
           }
            if ($result){
                echo json_encode(['code'=>0,'msg'=>'更新成功']);
            }else{
                echo json_encode(['code'=>-1,'msg'=>'暂无更新']);
            }
        }

    }


}